home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 1998 November / IRIX 6.5.2 Base Documentation November 1998.img / usr / share / catman / u_man / cat1 / perlfaq8.z / perlfaq8
Text File  |  1998-10-30  |  50KB  |  1,189 lines

  1.  
  2.  
  3.  
  4. PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))                                                        PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      perlfaq8 - System Interaction ($Revision: 1.21 $, $Date: 1997/04/24
  10.      22:44:19 $)
  11.  
  12. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  13.      This section of the Perl FAQ covers questions involving operating system
  14.      interaction.  This involves interprocess communication (IPC), control
  15.      over the user-interface (keyboard, screen and pointing devices), and most
  16.      anything else not related to data manipulation.
  17.  
  18.      Read the FAQs and documentation specific to the port of perl to your
  19.      operating system (eg, the _p_e_r_l_v_m_s manpage, the _p_e_r_l_p_l_a_n_9 manpage, ...).
  20.      These should contain more detailed information on the vagaries of your
  21.      perl.
  22.  
  23.      HHHHoooowwww ddddoooo IIII ffffiiiinnnndddd oooouuuutttt wwwwhhhhiiiicccchhhh ooooppppeeeerrrraaaattttiiiinnnngggg ssssyyyysssstttteeeemmmm IIII''''mmmm rrrruuuunnnnnnnniiiinnnngggg uuuunnnnddddeeeerrrr????
  24.  
  25.      The $^O variable ($OSTYPE if you use English) contains the operating
  26.      system that your perl binary was built for.
  27.  
  28.      HHHHoooowwww ccccoooommmmeeee _e_x_e_c() doesn't return?
  29.  
  30.      Because that's what it does: it replaces your currently running program
  31.      with a different one.  If you want to keep going (as is probably the case
  32.      if you're asking this question) use _s_y_s_t_e_m() instead.
  33.  
  34.      HHHHoooowwww ddddoooo IIII ddddoooo ffffaaaannnnccccyyyy ssssttttuuuuffffffff wwwwiiiitttthhhh tttthhhheeee kkkkeeeeyyyybbbbooooaaaarrrrdddd////ssssccccrrrreeeeeeeennnn////mmmmoooouuuusssseeee????
  35.  
  36.      How you access/control keyboards, screens, and pointing devices ("mice")
  37.      is system-dependent.  Try the following modules:
  38.  
  39.      Keyboard
  40.  
  41.              Term::Cap                   Standard perl distribution
  42.              Term::ReadKey               CPAN
  43.              Term::ReadLine::Gnu         CPAN
  44.              Term::ReadLine::Perl        CPAN
  45.              Term::Screen                CPAN
  46.  
  47.  
  48.      Screen
  49.  
  50.              Term::Cap                   Standard perl distribution
  51.              Curses                      CPAN
  52.              Term::ANSIColor             CPAN
  53.  
  54.  
  55.      Mouse
  56.  
  57.              Tk                          CPAN
  58.  
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))                                                        PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))
  71.  
  72.  
  73.  
  74.      HHHHoooowwww ddddoooo IIII aaaasssskkkk tttthhhheeee uuuusssseeeerrrr ffffoooorrrr aaaa ppppaaaasssssssswwwwoooorrrrdddd????
  75.  
  76.      (This question has nothing to do with the web.  See a different FAQ for
  77.      that.)
  78.  
  79.      There's an example of this in the crypt entry in the _p_e_r_l_f_u_n_c manpage).
  80.      First, you put the terminal into "no echo" mode, then just read the
  81.      password normally.  You may do this with an old-style _i_o_c_t_l() function,
  82.      POSIX terminal control (see the _P_O_S_I_X manpage, and Chapter 7 of the
  83.      Camel), or a call to the ssssttttttttyyyy program, with varying degrees of
  84.      portability.
  85.  
  86.      You can also do this for most systems using the Term::ReadKey module from
  87.      CPAN, which is easier to use and in theory more portable.
  88.  
  89.      HHHHoooowwww ddddoooo IIII rrrreeeeaaaadddd aaaannnndddd wwwwrrrriiiitttteeee tttthhhheeee sssseeeerrrriiiiaaaallll ppppoooorrrrtttt????
  90.  
  91.      This depends on which operating system your program is running on.  In
  92.      the case of Unix, the serial ports will be accessible through files in
  93.      /dev; on other systems, the devices names will doubtless differ.  Several
  94.      problem areas common to all device interaction are the following
  95.  
  96.      lockfiles
  97.          Your system may use lockfiles to control multiple access.  Make sure
  98.          you follow the correct protocol.  Unpredictable behaviour can result
  99.          from multiple processes reading from one device.
  100.  
  101.      open mode
  102.          If you expect to use both read and write operations on the device,
  103.          you'll have to open it for update (see the section on _o_p_e_n in the
  104.          _p_e_r_l_f_u_n_c manpage for details).  You may wish to open it without
  105.          running the risk of blocking by using _s_y_s_o_p_e_n() and
  106.          O_RDWR|O_NDELAY|O_NOCTTY from the Fcntl module (part of the standard
  107.          perl distribution).  See the section on _s_y_s_o_p_e_n in the _p_e_r_l_f_u_n_c
  108.          manpage for more on this approach.
  109.  
  110.      end of line
  111.          Some devices will be expecting a "\r" at the end of each line rather
  112.          than a "\n".  In some ports of perl, "\r" and "\n" are different from
  113.          their usual (Unix) ASCII values of "\012" and "\015".  You may have
  114.          to give the numeric values you want directly, using octal ("\015"),
  115.          hex ("0x0D"), or as a control-character specification ("\cM").
  116.  
  117.              print DEV "atv1\012";       # wrong, for some devices
  118.              print DEV "atv1\015";       # right, for some devices
  119.  
  120.          Even though with normal text files, a "\n" will do the trick, there
  121.          is still no unified scheme for terminating a line that is portable
  122.          between Unix, DOS/Win, and Macintosh, except to terminate _A_L_L line
  123.          ends with "\015\012", and strip what you don't need from the output.
  124.          This applies especially to socket I/O and autoflushing, discussed
  125.          next.
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))                                                        PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))
  137.  
  138.  
  139.  
  140.      flushing output
  141.          If you expect characters to get to your device when you _p_r_i_n_t() them,
  142.          you'll want to autoflush that filehandle, as in the older
  143.  
  144.              use FileHandle;
  145.              DEV->autoflush(1);
  146.  
  147.          and the newer
  148.  
  149.              use IO::Handle;
  150.              DEV->autoflush(1);
  151.  
  152.          You can use _s_e_l_e_c_t() and the $| variable to control autoflushing (see
  153.          the section on $| in the _p_e_r_l_v_a_r manpage and the select entry in the
  154.          _p_e_r_l_f_u_n_c manpage):
  155.  
  156.              $oldh = select(DEV);
  157.              $| = 1;
  158.              select($oldh);
  159.  
  160.          You'll also see code that does this without a temporary variable, as
  161.          in
  162.  
  163.              select((select(DEV), $| = 1)[0]);
  164.  
  165.          As mentioned in the previous item, this still doesn't work when using
  166.          socket I/O between Unix and Macintosh.  You'll need to hardcode your
  167.          line terminators, in that case.
  168.  
  169.      non-blocking input
  170.          If you are doing a blocking _r_e_a_d() or _s_y_s_r_e_a_d(), you'll have to
  171.          arrange for an alarm handler to provide a timeout (see the alarm
  172.          entry in the _p_e_r_l_f_u_n_c manpage).  If you have a non-blocking open,
  173.          you'll likely have a non-blocking read, which means you may have to
  174.          use a 4-arg _s_e_l_e_c_t() to determine whether I/O is ready on that device
  175.          (see the section on _s_e_l_e_c_t in the _p_e_r_l_f_u_n_c manpage.
  176.  
  177.      HHHHoooowwww ddddoooo IIII ddddeeeeccccooooddddeeee eeeennnnccccrrrryyyypppptttteeeedddd ppppaaaasssssssswwwwoooorrrrdddd ffffiiiilllleeeessss????
  178.  
  179.      You spend lots and lots of money on dedicated hardware, but this is bound
  180.      to get you talked about.
  181.  
  182.      Seriously, you can't if they are Unix password files - the Unix password
  183.      system employs one-way encryption.  Programs like Crack can forcibly (and
  184.      intelligently) try to guess passwords, but don't (can't) guarantee quick
  185.      success.
  186.  
  187.      If you're worried about users selecting bad passwords, you should
  188.      proactively check when they try to change their password (by modifying
  189.      _p_a_s_s_w_d(1), for example).
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))                                                        PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))
  203.  
  204.  
  205.  
  206.      HHHHoooowwww ddddoooo IIII ssssttttaaaarrrrtttt aaaa pppprrrroooocccceeeessssssss iiiinnnn tttthhhheeee bbbbaaaacccckkkkggggrrrroooouuuunnnndddd????
  207.  
  208.      You could use
  209.  
  210.          system("cmd &")
  211.  
  212.      or you could use fork as documented in the section on _f_o_r_k in the
  213.      _p_e_r_l_f_u_n_c manpage, with further examples in the _p_e_r_l_i_p_c manpage.  Some
  214.      things to be aware of, if you're on a Unix-like system:
  215.  
  216.      STDIN, STDOUT and STDERR are shared
  217.          Both the main process and the backgrounded one (the "child" process)
  218.          share the same STDIN, STDOUT and STDERR filehandles.  If both try to
  219.          access them at once, strange things can happen.  You may want to
  220.          close or reopen these for the child.  You can get around this with
  221.          opening a pipe (see the section on _o_p_e_n in the _p_e_r_l_f_u_n_c manpage) but
  222.          on some systems this means that the child process cannot outlive the
  223.          parent.
  224.  
  225.      Signals
  226.          You'll have to catch the SIGCHLD signal, and possibly SIGPIPE too.
  227.          SIGCHLD is sent when the backgrounded process finishes.  SIGPIPE is
  228.          sent when you write to a filehandle whose child process has closed
  229.          (an untrapped SIGPIPE can cause your program to silently die).  This
  230.          is not an issue with system("cmd&").
  231.  
  232.      Zombies
  233.          You have to be prepared to "reap" the child process when it finishes
  234.  
  235.              $SIG{CHLD} = sub { wait };
  236.  
  237.          See the section on _S_i_g_n_a_l_s in the _p_e_r_l_i_p_c manpage for other examples
  238.          of code to do this.  Zombies are not an issue with system("prog &").
  239.  
  240.      HHHHoooowwww ddddoooo IIII ttttrrrraaaapppp ccccoooonnnnttttrrrroooollll cccchhhhaaaarrrraaaacccctttteeeerrrrssss////ssssiiiiggggnnnnaaaallllssss????
  241.  
  242.      You don't actually "trap" a control character.  Instead, that character
  243.      generates a signal, which you then trap.  Signals are documented in the
  244.      section on _S_i_g_n_a_l_s in the _p_e_r_l_i_p_c manpage and chapter 6 of the Camel.
  245.  
  246.      Be warned that very few C libraries are re-entrant.  Therefore, if you
  247.      attempt to _p_r_i_n_t() in a handler that got invoked during another stdio
  248.      operation your internal structures will likely be in an inconsistent
  249.      state, and your program will dump core.  You can sometimes avoid this by
  250.      using _s_y_s_w_r_i_t_e() instead of _p_r_i_n_t().
  251.  
  252.      Unless you're exceedingly careful, the only safe things to do inside a
  253.      signal handler are: set a variable and exit.  And in the first case, you
  254.      should only set a variable in such a way that _m_a_l_l_o_c() is not called (eg,
  255.      by setting a variable that already has a value).
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))                                                        PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))
  269.  
  270.  
  271.  
  272.      For example:
  273.  
  274.          $Interrupted = 0;   # to ensure it has a value
  275.          $SIG{INT} = sub {
  276.              $Interrupted++;
  277.              syswrite(STDERR, "ouch\n", 5);
  278.          }
  279.  
  280.      However, because syscalls restart by default, you'll find that if you're
  281.      in a "slow" call, such as <FH>, _r_e_a_d(), _c_o_n_n_e_c_t(), or _w_a_i_t(), that the
  282.      only way to terminate them is by "longjumping" out; that is, by raising
  283.      an exception.  See the time-out handler for a blocking _f_l_o_c_k() in the
  284.      section on _S_i_g_n_a_l_s in the _p_e_r_l_i_p_c manpage or chapter 6 of the Camel.
  285.  
  286.      HHHHoooowwww ddddoooo IIII mmmmooooddddiiiiffffyyyy tttthhhheeee sssshhhhaaaaddddoooowwww ppppaaaasssssssswwwwoooorrrrdddd ffffiiiilllleeee oooonnnn aaaa UUUUnnnniiiixxxx ssssyyyysssstttteeeemmmm????
  287.  
  288.      If perl was installed correctly, the getpw*() functions described in the
  289.      _p_e_r_l_f_u_n_c manpage provide (read-only) access to the shadow password file.
  290.      To change the file, make a new shadow password file (the format varies
  291.      from system to system - see the _p_a_s_s_w_d(_5) manpage for specifics) and use
  292.      _p_w_d__m_k_d_b(8) to install it (see the _p_w_d__m_k_d_b(_5) manpage for more details).
  293.  
  294.      HHHHoooowwww ddddoooo IIII sssseeeetttt tttthhhheeee ttttiiiimmmmeeee aaaannnndddd ddddaaaatttteeee????
  295.  
  296.      Assuming you're running under sufficient permissions, you should be able
  297.      to set the system-wide date and time by running the _d_a_t_e(1) program.
  298.      (There is no way to set the time and date on a per-process basis.)  This
  299.      mechanism will work for Unix, MS-DOS, Windows, and NT; the VMS equivalent
  300.      is set time.
  301.  
  302.      However, if all you want to do is change your timezone, you can probably
  303.      get away with setting an environment variable:
  304.  
  305.          $ENV{TZ} = "MST7MDT";                  # unixish
  306.          $ENV{'SYS$TIMEZONE_DIFFERENTIAL'}="-5" # vms
  307.          system "trn comp.lang.perl";
  308.  
  309.  
  310.      HHHHoooowwww ccccaaaannnn IIII _s_l_e_e_p() or _a_l_a_r_m() for under a second?
  311.  
  312.      If you want finer granularity than the 1 second that the _s_l_e_e_p() function
  313.      provides, the easiest way is to use the _s_e_l_e_c_t() function as documented
  314.      in the section on _s_e_l_e_c_t in the _p_e_r_l_f_u_n_c manpage.  If your system has
  315.      itimers and _s_y_s_c_a_l_l() support, you can check out the old example in
  316.      http://www.perl.com/CPAN/doc/misc/ancient/tutorial/eg/itimers.pl .
  317.  
  318.      HHHHoooowwww ccccaaaannnn IIII mmmmeeeeaaaassssuuuurrrreeee ttttiiiimmmmeeee uuuunnnnddddeeeerrrr aaaa sssseeeeccccoooonnnndddd????
  319.  
  320.      In general, you may not be able to.  The Time::HiRes module (available
  321.      from CPAN) provides this functionality for some systems.
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))                                                        PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))
  335.  
  336.  
  337.  
  338.      In general, you may not be able to.  But if you system supports both the
  339.      _s_y_s_c_a_l_l() function in Perl as well as a system call like _g_e_t_t_i_m_e_o_f_d_a_y(2),
  340.      then you may be able to do something like this:
  341.  
  342.          require 'sys/syscall.ph';
  343.  
  344.          $TIMEVAL_T = "LL";
  345.  
  346.          $done = $start = pack($TIMEVAL_T, ());
  347.  
  348.          syscall( &SYS_gettimeofday, $start, 0)) != -1
  349.                     or die "gettimeofday: $!";
  350.  
  351.             ##########################
  352.             # DO YOUR OPERATION HERE #
  353.             ##########################
  354.  
  355.          syscall( &SYS_gettimeofday, $done, 0) != -1
  356.                 or die "gettimeofday: $!";
  357.  
  358.          @start = unpack($TIMEVAL_T, $start);
  359.          @done  = unpack($TIMEVAL_T, $done);
  360.  
  361.          # fix microseconds
  362.          for ($done[1], $start[1]) { $_ /= 1_000_000 }
  363.  
  364.          $delta_time = sprintf "%.4f", ($done[0]  + $done[1]  )
  365.                                                  -
  366.                                       ($start[0] + $start[1] );
  367.  
  368.  
  369.      HHHHoooowwww ccccaaaannnn IIII ddddoooo aaaannnn _a_t_e_x_i_t() or _s_e_t_j_m_p()/_l_o_n_g_j_m_p()? (Exception handling)
  370.  
  371.      Release 5 of Perl added the END block, which can be used to simulate
  372.      _a_t_e_x_i_t().  Each package's END block is called when the program or thread
  373.      ends (see the _p_e_r_l_m_o_d manpage manpage for more details).  It isn't called
  374.      when untrapped signals kill the program, though, so if you use END blocks
  375.      you should also use
  376.  
  377.              use sigtrap qw(die normal-signals);
  378.  
  379.      Perl's exception-handling mechanism is its _e_v_a_l() operator.  You can use
  380.      _e_v_a_l() as setjmp and _d_i_e() as longjmp.  For details of this, see the
  381.      section on signals, especially the time-out handler for a blocking
  382.      _f_l_o_c_k() in the section on _S_i_g_n_a_l_s in the _p_e_r_l_i_p_c manpage and chapter 6 of
  383.      the Camel.
  384.  
  385.      If exception handling is all you're interested in, try the exceptions.pl
  386.      library (part of the standard perl distribution).
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.                                                                         PPPPaaaaggggeeee 6666
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))                                                        PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))
  401.  
  402.  
  403.  
  404.      If you want the _a_t_e_x_i_t() syntax (and an _r_m_e_x_i_t() as well), try the AtExit
  405.      module available from CPAN.
  406.  
  407.      WWWWhhhhyyyy ddddooooeeeessssnnnn''''tttt mmmmyyyy ssssoooocccckkkkeeeettttssss pppprrrrooooggggrrrraaaammmm wwwwoooorrrrkkkk uuuunnnnddddeeeerrrr SSSSyyyysssstttteeeemmmm VVVV ((((SSSSoooollllaaaarrrriiiissss))))???? WWWWhhhhaaaatttt ddddooooeeeessss
  408.      tttthhhheeee eeeerrrrrrrroooorrrr mmmmeeeessssssssaaaaggggeeee """"PPPPrrrroooottttooooccccoooollll nnnnooootttt ssssuuuuppppppppoooorrrrtttteeeedddd"""" mmmmeeeeaaaannnn????
  409.  
  410.      Some Sys-V based systems, notably Solaris 2.X, redefined some of the
  411.      standard socket constants.  Since these were constant across all
  412.      architectures, they were often hardwired into perl code.  The proper way
  413.      to deal with this is to "use Socket" to get the correct values.
  414.  
  415.      Note that even though SunOS and Solaris are binary compatible, these
  416.      values are different.  Go figure.
  417.  
  418.      HHHHoooowwww ccccaaaannnn IIII ccccaaaallllllll mmmmyyyy ssssyyyysssstttteeeemmmm''''ssss uuuunnnniiiiqqqquuuueeee CCCC ffffuuuunnnnccccttttiiiioooonnnnssss ffffrrrroooommmm PPPPeeeerrrrllll????
  419.  
  420.      In most cases, you write an external module to do it - see the answer to
  421.      "Where can I learn about linking C with Perl? [h2xs, xsubpp]".  However,
  422.      if the function is a system call, and your system supports _s_y_s_c_a_l_l(), you
  423.      can use the syscall function (documented in the _p_e_r_l_f_u_n_c manpage).
  424.  
  425.      Remember to check the modules that came with your distribution, and CPAN
  426.      as well - someone may already have written a module to do it.
  427.  
  428.      WWWWhhhheeeerrrreeee ddddoooo IIII ggggeeeetttt tttthhhheeee iiiinnnncccclllluuuuddddeeee ffffiiiilllleeeessss ttttoooo ddddoooo _i_o_c_t_l() or _s_y_s_c_a_l_l()?
  429.  
  430.      Historically, these would be generated by the h2ph tool, part of the
  431.      standard perl distribution.  This program converts _c_p_p(1) directives in C
  432.      header files to files containing subroutine definitions, like
  433.      &SYS_getitimer, which you can use as arguments to your functions.  It
  434.      doesn't work perfectly, but it usually gets most of the job done.  Simple
  435.      files like _e_r_r_n_o._h, _s_y_s_c_a_l_l._h, and _s_o_c_k_e_t._h were fine, but the hard ones
  436.      like _i_o_c_t_l._h nearly always need to hand-edited.  Here's how to install
  437.      the *.ph files:
  438.  
  439.          1.  become super-user
  440.          2.  cd /usr/include
  441.          3.  h2ph *.h */*.h
  442.  
  443.      If your system supports dynamic loading, for reasons of portability and
  444.      sanity you probably ought to use h2xs (also part of the standard perl
  445.      distribution).  This tool converts C header files to Perl extensions.
  446.      See the _p_e_r_l_x_s_t_u_t manpage for how to get started with h2xs.
  447.  
  448.      If your system doesn't support dynamic loading, you still probably ought
  449.      to use h2xs.  See the _p_e_r_l_x_s_t_u_t manpage and the _E_x_t_U_t_i_l_s::_M_a_k_e_M_a_k_e_r
  450.      manpage for more information (in brief, just use mmmmaaaakkkkeeee ppppeeeerrrrllll instead of a
  451.      plain mmmmaaaakkkkeeee to rebuild perl with a new static extension).
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.                                                                         PPPPaaaaggggeeee 7777
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466. PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))                                                        PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))
  467.  
  468.  
  469.  
  470.      WWWWhhhhyyyy ddddoooo sssseeeettttuuuuiiiidddd ppppeeeerrrrllll ssssccccrrrriiiippppttttssss ccccoooommmmppppllllaaaaiiiinnnn aaaabbbboooouuuutttt kkkkeeeerrrrnnnneeeellll pppprrrroooobbbblllleeeemmmmssss????
  471.  
  472.      Some operating systems have bugs in the kernel that make setuid scripts
  473.      inherently insecure.  Perl gives you a number of options (described in
  474.      the _p_e_r_l_s_e_c manpage) to work around such systems.
  475.  
  476.      HHHHoooowwww ccccaaaannnn IIII ooooppppeeeennnn aaaa ppppiiiippppeeee bbbbooootttthhhh ttttoooo aaaannnndddd ffffrrrroooommmm aaaa ccccoooommmmmmmmaaaannnndddd????
  477.  
  478.      The IPC::Open2 module (part of the standard perl distribution) is an
  479.      easy-to-use approach that internally uses _p_i_p_e(), _f_o_r_k(), and _e_x_e_c() to
  480.      do the job.  Make sure you read the deadlock warnings in its
  481.      documentation, though (see the _I_P_C::_O_p_e_n_2 manpage).
  482.  
  483.      WWWWhhhhyyyy ccccaaaannnn''''tttt IIII ggggeeeetttt tttthhhheeee oooouuuuttttppppuuuutttt ooooffff aaaa ccccoooommmmmmmmaaaannnndddd wwwwiiiitttthhhh _s_y_s_t_e_m()?
  484.  
  485.      You're confusing the purpose of _s_y_s_t_e_m() and backticks (``).  _s_y_s_t_e_m()
  486.      runs a command and returns exit status information (as a 16 bit value:
  487.      the low 8 bits are the signal the process died from, if any, and the high
  488.      8 bits are the actual exit value).  Backticks (``) run a command and
  489.      return what it sent to STDOUT.
  490.  
  491.          $exit_status   = system("mail-users");
  492.          $output_string = `ls`;
  493.  
  494.  
  495.      HHHHoooowwww ccccaaaannnn IIII ccccaaaappppttttuuuurrrreeee SSSSTTTTDDDDEEEERRRRRRRR ffffrrrroooommmm aaaannnn eeeexxxxtttteeeerrrrnnnnaaaallll ccccoooommmmmmmmaaaannnndddd????
  496.  
  497.      There are three basic ways of running external commands:
  498.  
  499.          system $cmd;                # using system()
  500.          $output = `$cmd`;           # using backticks (``)
  501.          open (PIPE, "cmd |");       # using open()
  502.  
  503.      With _s_y_s_t_e_m(), both STDOUT and STDERR will go the same place as the
  504.      script's versions of these, unless the command redirects them.  Backticks
  505.      and _o_p_e_n() read oooonnnnllllyyyy the STDOUT of your command.
  506.  
  507.      With any of these, you can change file descriptors before the call:
  508.  
  509.          open(STDOUT, ">logfile");
  510.          system("ls");
  511.  
  512.      or you can use Bourne shell file-descriptor redirection:
  513.  
  514.          $output = `$cmd 2>some_file`;
  515.          open (PIPE, "cmd 2>some_file |");
  516.  
  517.      You can also use file-descriptor redirection to make STDERR a duplicate
  518.      of STDOUT:
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.                                                                         PPPPaaaaggggeeee 8888
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))                                                        PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))
  533.  
  534.  
  535.  
  536.          $output = `$cmd 2>&1`;
  537.          open (PIPE, "cmd 2>&1 |");
  538.  
  539.      Note that you _c_a_n_n_o_t simply open STDERR to be a dup of STDOUT in your
  540.      Perl program and avoid calling the shell to do the redirection.  This
  541.      doesn't work:
  542.  
  543.          open(STDERR, ">&STDOUT");
  544.          $alloutput = `cmd args`;  # stderr still escapes
  545.  
  546.      This fails because the _o_p_e_n() makes STDERR go to where STDOUT was going
  547.      at the time of the _o_p_e_n().  The backticks then make STDOUT go to a
  548.      string, but don't change STDERR (which still goes to the old STDOUT).
  549.  
  550.      Note that you _m_u_s_t use Bourne shell (_s_h(1)) redirection syntax in
  551.      backticks, not _c_s_h(1)!  Details on why Perl's _s_y_s_t_e_m() and backtick and
  552.      pipe opens all use the Bourne shell are in
  553.      http://www.perl.com/CPAN/doc/FMTEYEWTK/versus/csh.whynot .
  554.  
  555.      You may also use the IPC::Open3 module (part of the standard perl
  556.      distribution), but be warned that it has a different order of arguments
  557.      from IPC::Open2 (see the _I_P_C::_O_p_e_n_3 manpage).
  558.  
  559.      WWWWhhhhyyyy ddddooooeeeessssnnnn''''tttt _o_p_e_n() return an error when a pipe open fails?
  560.  
  561.      It does, but probably not how you expect it to.  On systems that follow
  562.      the standard _f_o_r_k()/_e_x_e_c() paradigm (eg, Unix), it works like this:
  563.      _o_p_e_n() causes a _f_o_r_k().  In the parent, _o_p_e_n() returns with the process
  564.      ID of the child.  The child _e_x_e_c()s the command to be piped to/from.  The
  565.      parent can't know whether the _e_x_e_c() was successful or not - all it can
  566.      return is whether the _f_o_r_k() succeeded or not.  To find out if the
  567.      command succeeded, you have to catch SIGCHLD and _w_a_i_t() to get the exit
  568.      status.  You should also catch SIGPIPE if you're writing to the child --
  569.      you may not have found out the _e_x_e_c() failed by the time you write.  This
  570.      is documented in the _p_e_r_l_i_p_c manpage.
  571.  
  572.      On systems that follow the _s_p_a_w_n() paradigm, _o_p_e_n() _m_i_g_h_t do what you
  573.      expect - unless perl uses a shell to start your command. In this case the
  574.      _f_o_r_k()/_e_x_e_c() description still applies.
  575.  
  576.      WWWWhhhhaaaatttt''''ssss wwwwrrrroooonnnngggg wwwwiiiitttthhhh uuuussssiiiinnnngggg bbbbaaaacccckkkkttttiiiicccckkkkssss iiiinnnn aaaa vvvvooooiiiidddd ccccoooonnnntttteeeexxxxtttt????
  577.  
  578.      Strictly speaking, nothing.  Stylistically speaking, it's not a good way
  579.      to write maintainable code because backticks have a (potentially
  580.      humungous) return value, and you're ignoring it.  It's may also not be
  581.      very efficient, because you have to read in all the lines of output,
  582.      allocate memory for them, and then throw it away.  Too often people are
  583.      lulled to writing:
  584.  
  585.          `cp file file.bak`;
  586.  
  587.      And now they think "Hey, I'll just always use backticks to run programs."
  588.  
  589.  
  590.  
  591.                                                                         PPPPaaaaggggeeee 9999
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))                                                        PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))
  599.  
  600.  
  601.  
  602.      Bad idea: backticks are for capturing a program's output; the _s_y_s_t_e_m()
  603.      function is for running programs.
  604.  
  605.      Consider this line:
  606.  
  607.          `cat /etc/termcap`;
  608.  
  609.      You haven't assigned the output anywhere, so it just wastes memory (for a
  610.      little while).  Plus you forgot to check $? to see whether the program
  611.      even ran correctly.  Even if you wrote
  612.  
  613.          print `cat /etc/termcap`;
  614.  
  615.      In most cases, this could and probably should be written as
  616.  
  617.          system("cat /etc/termcap") == 0
  618.              or die "cat program failed!";
  619.  
  620.      Which will get the output quickly (as its generated, instead of only at
  621.      the end ) and also check the return value.
  622.  
  623.      _s_y_s_t_e_m() also provides direct control over whether shell wildcard
  624.      processing may take place, whereas backticks do not.
  625.  
  626.      HHHHoooowwww ccccaaaannnn IIII ccccaaaallllllll bbbbaaaacccckkkkttttiiiicccckkkkssss wwwwiiiitttthhhhoooouuuutttt sssshhhheeeellllllll pppprrrroooocccceeeessssssssiiiinnnngggg????
  627.  
  628.      This is a bit tricky.  Instead of writing
  629.  
  630.          @ok = `grep @opts '$search_string' @filenames`;
  631.  
  632.      You have to do this:
  633.  
  634.          my @ok = ();
  635.          if (open(GREP, "-|")) {
  636.              while (<GREP>) {
  637.                  chomp;
  638.                  push(@ok, $_);
  639.              }
  640.              close GREP;
  641.          } else {
  642.              exec 'grep', @opts, $search_string, @filenames;
  643.          }
  644.  
  645.      Just as with _s_y_s_t_e_m(), no shell escapes happen when you _e_x_e_c() a list.
  646.  
  647.      WWWWhhhhyyyy ccccaaaannnn''''tttt mmmmyyyy ssssccccrrrriiiipppptttt rrrreeeeaaaadddd ffffrrrroooommmm SSSSTTTTDDDDIIIINNNN aaaafffftttteeeerrrr IIII ggggaaaavvvveeee iiiitttt EEEEOOOOFFFF ((((^^^^DDDD oooonnnn UUUUnnnniiiixxxx,,,, ^^^^ZZZZ
  648.      oooonnnn MMMMSSSS----DDDDOOOOSSSS))))????
  649.  
  650.      Because some stdio's set error and eof flags that need clearing.  The
  651.      POSIX module defines _c_l_e_a_r_e_r_r() that you can use.  That is the
  652.      technically correct way to do it.  Here are some less reliable
  653.      workarounds:
  654.  
  655.  
  656.  
  657.                                                                        PPPPaaaaggggeeee 11110000
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664. PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))                                                        PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))
  665.  
  666.  
  667.  
  668.      1   Try keeping around the seekpointer and go there, like this:
  669.  
  670.              $where = tell(LOG);
  671.              seek(LOG, $where, 0);
  672.  
  673.  
  674.      2   If that doesn't work, try seeking to a different part of the file and
  675.          then back.
  676.  
  677.      3   If that doesn't work, try seeking to a different part of the file,
  678.          reading something, and then seeking back.
  679.  
  680.      4   If that doesn't work, give up on your stdio package and use sysread.
  681.  
  682.      HHHHoooowwww ccccaaaannnn IIII ccccoooonnnnvvvveeeerrrrtttt mmmmyyyy sssshhhheeeellllllll ssssccccrrrriiiipppptttt ttttoooo ppppeeeerrrrllll????
  683.  
  684.      Learn Perl and rewrite it.  Seriously, there's no simple converter.
  685.      Things that are awkward to do in the shell are easy to do in Perl, and
  686.      this very awkwardness is what would make a shell->perl converter nigh-on
  687.      impossible to write.  By rewriting it, you'll think about what you're
  688.      really trying to do, and hopefully will escape the shell's pipeline
  689.      datastream paradigm, which while convenient for some matters, causes many
  690.      inefficiencies.
  691.  
  692.      CCCCaaaannnn IIII uuuusssseeee ppppeeeerrrrllll ttttoooo rrrruuuunnnn aaaa tttteeeellllnnnneeeetttt oooorrrr ffffttttpppp sssseeeessssssssiiiioooonnnn????
  693.  
  694.      Try the Net::FTP, TCP::Client, and Net::Telnet modules (available from
  695.      CPAN).  http://www.perl.com/CPAN/scripts/netstuff/telnet.emul.shar will
  696.      also help for emulating the telnet protocol, but Net::Telnet is quite
  697.      probably easier to use..
  698.  
  699.      If all you want to do is pretend to be telnet but don't need the initial
  700.      telnet handshaking, then the standard dual-process approach will suffice:
  701.  
  702.          use IO::Socket;             # new in 5.004
  703.          $handle = IO::Socket::INET->new('www.perl.com:80')
  704.                  || die "can't connect to port 80 on www.perl.com: $!";
  705.          $handle->autoflush(1);
  706.          if (fork()) {               # XXX: undef means failure
  707.              select($handle);
  708.              print while <STDIN>;    # everything from stdin to socket
  709.          } else {
  710.              print while <$handle>;  # everything from socket to stdout
  711.          }
  712.          close $handle;
  713.          exit;
  714.  
  715.  
  716.      HHHHoooowwww ccccaaaannnn IIII wwwwrrrriiiitttteeee eeeexxxxppppeeeecccctttt iiiinnnn PPPPeeeerrrrllll????
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.                                                                        PPPPaaaaggggeeee 11111111
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730. PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))                                                        PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))
  731.  
  732.  
  733.  
  734.      Once upon a time, there was a library called chat2.pl (part of the
  735.      standard perl distribution), which never really got finished.  These
  736.      days, your best bet is to look at the Comm.pl library available from
  737.      CPAN.
  738.  
  739.      IIIIssss tttthhhheeeerrrreeee aaaa wwwwaaaayyyy ttttoooo hhhhiiiiddddeeee ppppeeeerrrrllll''''ssss ccccoooommmmmmmmaaaannnndddd lllliiiinnnneeee ffffrrrroooommmm pppprrrrooooggggrrrraaaammmmssss ssssuuuucccchhhh aaaassss """"ppppssss""""????
  740.  
  741.      First of all note that if you're doing this for security reasons (to
  742.      avoid people seeing passwords, for example) then you should rewrite your
  743.      program so that critical information is never given as an argument.
  744.      Hiding the arguments won't make your program completely secure.
  745.  
  746.      To actually alter the visible command line, you can assign to the
  747.      variable $0 as documented in the _p_e_r_l_v_a_r manpage.  This won't work on all
  748.      operating systems, though.  Daemon programs like sendmail place their
  749.      state there, as in:
  750.  
  751.          $0 = "orcus [accepting connections]";
  752.  
  753.  
  754.      IIII {{{{cccchhhhaaaannnnggggeeeedddd ddddiiiirrrreeeeccccttttoooorrrryyyy,,,, mmmmooooddddiiiiffffiiiieeeedddd mmmmyyyy eeeennnnvvvviiiirrrroooonnnnmmmmeeeennnntttt}}}} iiiinnnn aaaa ppppeeeerrrrllll ssssccccrrrriiiipppptttt....  HHHHoooowwww
  755.      ccccoooommmmeeee tttthhhheeee cccchhhhaaaannnnggggeeee ddddiiiissssaaaappppppppeeeeaaaarrrreeeedddd wwwwhhhheeeennnn IIII eeeexxxxiiiitttteeeedddd tttthhhheeee ssssccccrrrriiiipppptttt????  HHHHoooowwww ddddoooo IIII ggggeeeetttt mmmmyyyy
  756.      cccchhhhaaaannnnggggeeeessss ttttoooo bbbbeeee vvvviiiissssiiiibbbblllleeee????
  757.  
  758.      Unix
  759.          In the strictest sense, it can't be done -- the script executes as a
  760.          different process from the shell it was started from.  Changes to a
  761.          process are not reflected in its parent, only in its own children
  762.          created after the change.  There is shell magic that may allow you to
  763.          fake it by _e_v_a_l()ing the script's output in your shell; check out the
  764.          comp.unix.questions FAQ for details.
  765.  
  766.      VMS Change to %ENV persist after Perl exits, but directory changes do
  767.          not.
  768.  
  769.      HHHHoooowwww ddddoooo IIII cccclllloooosssseeee aaaa pppprrrroooocccceeeessssssss''''ssss ffffiiiilllleeeehhhhaaaannnnddddlllleeee wwwwiiiitttthhhhoooouuuutttt wwwwaaaaiiiittttiiiinnnngggg ffffoooorrrr iiiitttt ttttoooo ccccoooommmmpppplllleeeetttteeee????
  770.  
  771.      Assuming your system supports such things, just send an appropriate
  772.      signal to the process (see the section on _k_i_l_l in the _p_e_r_l_f_u_n_c manpage.
  773.      It's common to first send a TERM signal, wait a little bit, and then send
  774.      a KILL signal to finish it off.
  775.  
  776.      HHHHoooowwww ddddoooo IIII ffffoooorrrrkkkk aaaa ddddaaaaeeeemmmmoooonnnn pppprrrroooocccceeeessssssss????
  777.  
  778.      If by daemon process you mean one that's detached (disassociated from its
  779.      tty), then the following process is reported to work on most Unixish
  780.      systems.  Non-Unix users should check their Your_OS::Process module for
  781.      other solutions.
  782.  
  783.      +o   Open /dev/tty and use the the TIOCNOTTY ioctl on it.  See the _t_t_y(_4)
  784.          manpage for details.
  785.  
  786.  
  787.  
  788.  
  789.                                                                        PPPPaaaaggggeeee 11112222
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796. PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))                                                        PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))
  797.  
  798.  
  799.  
  800.      +o   Change directory to /
  801.  
  802.      +o   Reopen STDIN, STDOUT, and STDERR so they're not connected to the old
  803.          tty.
  804.  
  805.      +o   Background yourself like this:
  806.  
  807.              fork && exit;
  808.  
  809.  
  810.      HHHHoooowwww ddddoooo IIII mmmmaaaakkkkeeee mmmmyyyy pppprrrrooooggggrrrraaaammmm rrrruuuunnnn wwwwiiiitttthhhh sssshhhh aaaannnndddd ccccsssshhhh????
  811.  
  812.      See the _e_g/_n_i_h script (part of the perl source distribution).
  813.  
  814.      HHHHoooowwww ddddoooo IIII ffffiiiinnnndddd oooouuuutttt iiiiffff IIII''''mmmm rrrruuuunnnnnnnniiiinnnngggg iiiinnnntttteeeerrrraaaaccccttttiiiivvvveeeellllyyyy oooorrrr nnnnooootttt????
  815.  
  816.      Good question.  Sometimes -t STDIN and -t STDOUT can give clues,
  817.      sometimes not.
  818.  
  819.          if (-t STDIN && -t STDOUT) {
  820.              print "Now what? ";
  821.          }
  822.  
  823.      On POSIX systems, you can test whether your own process group matches the
  824.      current process group of your controlling terminal as follows:
  825.  
  826.          use POSIX qw/getpgrp tcgetpgrp/;
  827.          open(TTY, "/dev/tty") or die $!;
  828.          $tpgrp = tcgetpgrp(TTY);
  829.          $pgrp = getpgrp();
  830.          if ($tpgrp == $pgrp) {
  831.              print "foreground\n";
  832.          } else {
  833.              print "background\n";
  834.          }
  835.  
  836.  
  837.      HHHHoooowwww ddddoooo IIII ttttiiiimmmmeeeeoooouuuutttt aaaa sssslllloooowwww eeeevvvveeeennnntttt????
  838.  
  839.      Use the _a_l_a_r_m() function, probably in conjunction with a signal handler,
  840.      as documented the section on _S_i_g_n_a_l_s in the _p_e_r_l_i_p_c manpage and chapter 6
  841.      of the Camel.  You may instead use the more flexible Sys::AlarmCall
  842.      module available from CPAN.
  843.  
  844.      HHHHoooowwww ddddoooo IIII sssseeeetttt CCCCPPPPUUUU lllliiiimmmmiiiittttssss????
  845.  
  846.      Use the BSD::Resource module from CPAN.
  847.  
  848.      HHHHoooowwww ddddoooo IIII aaaavvvvooooiiiidddd zzzzoooommmmbbbbiiiieeeessss oooonnnn aaaa UUUUnnnniiiixxxx ssssyyyysssstttteeeemmmm????
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.                                                                        PPPPaaaaggggeeee 11113333
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862. PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))                                                        PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))
  863.  
  864.  
  865.  
  866.      Use the reaper code from the section on _S_i_g_n_a_l_s in the _p_e_r_l_i_p_c manpage to
  867.      call _w_a_i_t() when a SIGCHLD is received, or else use the double-fork
  868.      technique described in the fork entry in the _p_e_r_l_f_u_n_c manpage.
  869.  
  870.      HHHHoooowwww ddddoooo IIII uuuusssseeee aaaannnn SSSSQQQQLLLL ddddaaaattttaaaabbbbaaaasssseeee????
  871.  
  872.      There are a number of excellent interfaces to SQL databases.  See the
  873.      DBD::* modules available from http://www.perl.com/CPAN/modules/dbperl/DBD
  874.      .
  875.  
  876.      HHHHoooowwww ddddoooo IIII mmmmaaaakkkkeeee aaaa _s_y_s_t_e_m() exit on control-C?
  877.  
  878.      You can't.  You need to imitate the _s_y_s_t_e_m() call (see the _p_e_r_l_i_p_c
  879.      manpage for sample code) and then have a signal handler for the INT
  880.      signal that passes the signal on to the subprocess.
  881.  
  882.      HHHHoooowwww ddddoooo IIII ooooppppeeeennnn aaaa ffffiiiilllleeee wwwwiiiitttthhhhoooouuuutttt bbbblllloooocccckkkkiiiinnnngggg????
  883.  
  884.      If you're lucky enough to be using a system that supports non-blocking
  885.      reads (most Unixish systems do), you need only to use the O_NDELAY or
  886.      O_NONBLOCK flag from the Fcntl module in conjunction with _s_y_s_o_p_e_n():
  887.  
  888.          use Fcntl;
  889.          sysopen(FH, "/tmp/somefile", O_WRONLY|O_NDELAY|O_CREAT, 0644)
  890.              or die "can't open /tmp/somefile: $!":
  891.  
  892.  
  893.      HHHHoooowwww ddddoooo IIII iiiinnnnssssttttaaaallllllll aaaa CCCCPPPPAAAANNNN mmmmoooodddduuuulllleeee????
  894.  
  895.      The easiest way is to have the CPAN module do it for you.  This module
  896.      comes with perl version 5.004 and later.  To manually install the CPAN
  897.      module, or any well-behaved CPAN module for that matter, follow these
  898.      steps:
  899.  
  900.      1   Unpack the source into a temporary area.
  901.  
  902.      2
  903.  
  904.              perl Makefile.PL
  905.  
  906.  
  907.      3
  908.  
  909.              make
  910.  
  911.  
  912.      4
  913.  
  914.              make test
  915.  
  916.  
  917.  
  918.  
  919.  
  920.  
  921.                                                                        PPPPaaaaggggeeee 11114444
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928. PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))                                                        PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))
  929.  
  930.  
  931.  
  932.      5
  933.  
  934.              make install
  935.  
  936.  
  937.      If your version of perl is compiled without dynamic loading, then you
  938.      just need to replace step 3 (mmmmaaaakkkkeeee) with mmmmaaaakkkkeeee ppppeeeerrrrllll and you will get a new
  939.      _p_e_r_l binary with your extension linked in.
  940.  
  941.      See the _E_x_t_U_t_i_l_s::_M_a_k_e_M_a_k_e_r manpage for more details on building
  942.      extensions, the question "How do I keep my own module/library directory?"
  943.  
  944.      HHHHoooowwww ddddoooo IIII kkkkeeeeeeeepppp mmmmyyyy oooowwwwnnnn mmmmoooodddduuuulllleeee////lllliiiibbbbrrrraaaarrrryyyy ddddiiiirrrreeeeccccttttoooorrrryyyy????
  945.  
  946.      When you build modules, use the PREFIX option when generating Makefiles:
  947.  
  948.          perl Makefile.PL PREFIX=/u/mydir/perl
  949.  
  950.      then either set the PERL5LIB environment variable before you run scripts
  951.      that use the modules/libraries (see the _p_e_r_l_r_u_n manpage) or say
  952.  
  953.          use lib '/u/mydir/perl';
  954.  
  955.      See Perl's the _l_i_b manpage for more information.
  956.  
  957.      HHHHoooowwww ddddoooo IIII aaaadddddddd tttthhhheeee ddddiiiirrrreeeeccccttttoooorrrryyyy mmmmyyyy pppprrrrooooggggrrrraaaammmm lllliiiivvvveeeessss iiiinnnn ttttoooo tttthhhheeee mmmmoooodddduuuulllleeee////lllliiiibbbbrrrraaaarrrryyyy
  958.      sssseeeeaaaarrrrcccchhhh ppppaaaatttthhhh????
  959.  
  960.          use FindBin;
  961.          use lib "$FindBin:Bin";
  962.          use your_own_modules;
  963.  
  964.  
  965.      HHHHoooowwww ddddoooo IIII aaaadddddddd aaaa ddddiiiirrrreeeeccccttttoooorrrryyyy ttttoooo mmmmyyyy iiiinnnncccclllluuuuddddeeee ppppaaaatttthhhh aaaatttt rrrruuuunnnnttttiiiimmmmeeee????
  966.  
  967.      Here are the suggested ways of modifying your include path:
  968.  
  969.          the PERLLIB environment variable
  970.          the PERL5LIB environment variable
  971.          the perl -Idir commpand line flag
  972.          the use lib pragma, as in
  973.              use lib "$ENV{HOME}/myown_perllib";
  974.  
  975.      The latter is particularly useful because it knows about machine
  976.      dependent architectures.  The lib.pm pragmatic module was first included
  977.      with the 5.002 release of Perl.
  978.  
  979. HHHHoooowwww ddddoooo IIII ggggeeeetttt oooonnnneeee kkkkeeeeyyyy ffffrrrroooommmm tttthhhheeee tttteeeerrrrmmmmiiiinnnnaaaallll aaaatttt aaaa ttttiiiimmmmeeee,,,, uuuunnnnddddeeeerrrr PPPPOOOOSSSSIIIIXXXX????
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.                                                                        PPPPaaaaggggeeee 11115555
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994. PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))                                                        PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))
  995.  
  996.  
  997.  
  998.          #!/usr/bin/perl -w
  999.          use strict;
  1000.          $| = 1;
  1001.          for (1..4) {
  1002.              my $got;
  1003.              print "gimme: ";
  1004.              $got = getone();
  1005.              print "--> $got\n";
  1006.          }
  1007.          exit;
  1008.  
  1009.          BEGIN {
  1010.              use POSIX qw(:termios_h);
  1011.  
  1012.              my ($term, $oterm, $echo, $noecho, $fd_stdin);
  1013.  
  1014.              $fd_stdin = fileno(STDIN);
  1015.  
  1016.              $term     = POSIX::Termios->new();
  1017.              $term->getattr($fd_stdin);
  1018.              $oterm     = $term->getlflag();
  1019.  
  1020.              $echo     = ECHO | ECHOK | ICANON;
  1021.              $noecho   = $oterm & ~$echo;
  1022.  
  1023.              sub cbreak {
  1024.                  $term->setlflag($noecho);
  1025.                  $term->setcc(VTIME, 1);
  1026.                  $term->setattr($fd_stdin, TCSANOW);
  1027.              }
  1028.  
  1029.              sub cooked {
  1030.                  $term->setlflag($oterm);
  1031.                  $term->setcc(VTIME, 0);
  1032.                  $term->setattr($fd_stdin, TCSANOW);
  1033.              }
  1034.  
  1035.              sub getone {
  1036.                  my $key = '';
  1037.                  cbreak();
  1038.                  sysread(STDIN, $key, 1);
  1039.                  cooked();
  1040.                  return $key;
  1041.              }
  1042.  
  1043.          }
  1044.          END { cooked() }
  1045.  
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.                                                                        PPPPaaaaggggeeee 11116666
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060. PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))                                                        PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))
  1061.  
  1062.  
  1063.  
  1064. AUTHOR AND COPYRIGHT
  1065.      Copyright (c) 1997 Tom Christiansen and Nathan Torkington.  All rights
  1066.      reserved.  See the _p_e_r_l_f_a_q manpage for distribution information.
  1067.  
  1068.  
  1069.  
  1070.  
  1071.  
  1072.  
  1073.  
  1074.  
  1075.  
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093.  
  1094.  
  1095.  
  1096.  
  1097.  
  1098.  
  1099.  
  1100.  
  1101.  
  1102.  
  1103.  
  1104.  
  1105.  
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119.                                                                        PPPPaaaaggggeeee 11117777
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126. PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))                                                        PPPPEEEERRRRLLLLFFFFAAAAQQQQ8888((((1111))))
  1127.  
  1128.  
  1129.  
  1130.  
  1131.  
  1132.  
  1133.  
  1134.  
  1135.  
  1136.  
  1137.  
  1138.  
  1139.  
  1140.  
  1141.  
  1142.  
  1143.  
  1144.  
  1145.  
  1146.  
  1147.  
  1148.  
  1149.  
  1150.  
  1151.  
  1152.  
  1153.  
  1154.  
  1155.  
  1156.  
  1157.  
  1158.  
  1159.  
  1160.  
  1161.  
  1162.  
  1163.  
  1164.  
  1165.  
  1166.  
  1167.  
  1168.  
  1169.  
  1170.  
  1171.  
  1172.  
  1173.  
  1174.  
  1175.  
  1176.  
  1177.  
  1178.  
  1179.  
  1180.  
  1181.  
  1182.                                                                        PPPPaaaaggggeeee 11118888
  1183.  
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189.